package classpath

import "archive/zip"
import "errors"
import "io/ioutil"
import "path/filepath"

type ZipEntry struct {
    absPath string
}

//创建ZipEntry实例
func newZipEntry(path string) *ZipEntry {
    absPath, err := filepath.Abs(path)
    if err != nil {
        panic(err)
    }

    return &ZipEntry{absPath}
}

//加载ZipEntry,读取class文件
func (self *ZipEntry) readClass(className string) ([]byte, Entry, error) {
    //读取zip文件
    r, err := zip.OpenReader(self.absPath)
    if err != nil {
        return nil, nil, err
    }
    defer r.Close()

    for _, f := range r.File {
        if f.Name == className {
            rc, err := f.Open()
            if err != nil {
                return nil, nil, err
            }
            defer rc.Close()
            data, err := ioutil.ReadAll(rc)
            if err != nil {
                return nil, nil, err
            }
            return data, self, nil
        }
    }

    return nil, nil, errors.New("类未找到: " + className)
}

//toString()方法
func (self *ZipEntry ) String() string {
    return self.absPath
}